** ---------------------------------- **												
* Author: Abdulrazzak Tamim 												
* First version: 18/4/2021												
* Last modified: 26/4/2024							
** ---------------------------------- **												
												
												
* User must define a global macro in order to run the program:												
* "path" contains the raw data												
												
												
clear all												
set more off												
program drop _all	

if "`c(username)'"=="abdulrazzaktamim" {															
gl path "/Users/abdulrazzaktamim/Library/CloudStorage/Dropbox/SoilDoc-Abdulrazzak"												
}
							
																							
program main

	regress_14mmp

end


program regress_14mmp

	use "$path/replication/data/organic_labor.dta", clear

	matrix A_1 = J(5,1,.) 
	matrix B_1 = J(5,1,.) 
	matrix C_1 = J(5,1,.) 
	matrix D_1 = J(5,1,.) 
	matrix A_2 = J(5,1,.) 
	matrix B_2 = J(5,1,.) 
	matrix C_2 = J(5,1,.) 
	matrix D_2 = J(5,1,.) 
	matrix A_3 = J(5,1,.) 
	matrix B_3 = J(5,1,.) 
	matrix C_3 = J(5,1,.) 
	matrix D_3 = J(5,1,.) 
	
	xtreg org_fert_2016 i.trt2 org_fert_2014 if gps_2016!=. /// 
	& mz_kg_acre_q30_2016!=., r fe cl(village) 
		outreg2 using "$path/replication/output/TableA19.doc", append ///
			ctitle(Binary) bd(2) sd(2) nocons addtext(Village FE, YES)
		su org_fert_2014 if e(sample) & trt2==1
		mat A_1[1,1] = r(mean)
		mat A_1[2,1] = r(sd)
		test 2.trt2=3.trt2
		mat A_1[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat A_1[4,1] = r(p)
		test 3.trt2=4.trt2
		mat A_1[5,1] = r(p)		
		matmap A_1  AN_1, map(round(@,0.01))
	
	xtreg manure_acres_2016 i.trt2 manure_acres_2014 if gps_2016!=. /// 
	& mz_kg_acre_q30_2016!=., r fe cl(village) 
		outreg2 using "$path/replication/output/TableA19.doc", append ///
			ctitle(Manure) bd(2) sd(2) nocons addtext(Village FE, YES)
		su manure_acres_2014 if e(sample) & trt2==1
		mat B_1[1,1] = r(mean)
		mat B_1[2,1] = r(sd)	
		test 2.trt2=3.trt2
		mat B_1[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat B_1[4,1] = r(p)
		test 3.trt2=4.trt2
		mat B_1[5,1] = r(p)	
		matmap B_1  BN_1, map(round(@,0.01))
		
	xtreg crop_residue_acres_2016 i.trt2 crop_residue_acres_2014 if gps_2016!=. /// 
	& mz_kg_acre_q30_2016!=., r fe cl(village) 
		outreg2 using "$path/replication/output/TableA19.doc", append ///
			ctitle(Residue) bd(2) sd(2) nocons addtext(Village FE, YES)
		su crop_residue_acres_2014 if e(sample) & trt2==1
		mat C_1[1,1] = r(mean)
		mat C_1[2,1] = r(sd)	
		test 2.trt2=3.trt2
		mat C_1[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat C_1[4,1] = r(p)
		test 3.trt2=4.trt2
		mat C_1[5,1] = r(p)	
		matmap C_1  CN_1, map(round(@,0.01))
		
	xtreg tot_org_acres_2016 i.trt2 tot_org_acres_2014 if gps_2016!=. /// 
	& mz_kg_acre_q30_2016!=., r fe cl(village) 
		outreg2 using "$path/replication/output/TableA19.doc", append ///
			ctitle(Both) bd(2) sd(2) nocons addtext(Village FE, YES)
		su tot_org_acres_2014 if e(sample) & trt2==1
		mat D_1[1,1] = r(mean)
		mat D_1[2,1] = r(sd)	
		test 2.trt2=3.trt2
		mat D_1[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat D_1[4,1] = r(p)
		test 3.trt2=4.trt2
		mat D_1[5,1] = r(p)	
		matmap D_1  DN_1, map(round(@,0.01))
	
	putexcel set "$path/replication/output/TableA19_F_tests.xls", sheet("F-tests") replace
	putexcel B2=matrix(AN_1) 
	putexcel C2=matrix(BN_1)
	putexcel D2=matrix(CN_1)
	putexcel E2=matrix(DN_1)	
	
	putexcel B1 = "binary"
	putexcel C1 = "manure area"
	putexcel D1 = "residue"
	putexcel E1 = "total"
	putexcel A2 = "CONTROL MEAN"
	putexcel A3 = "CONTROL SD"
	putexcel A4 = "V_vs_R"
	putexcel A5 = "V_vs_RV"
	putexcel A6 = "R_vs_RV"
	
		
	xtreg res_work_days_acre_2016 i.trt2 res_work_days_acre_2014 if gps_2016!=. /// 
	& mz_kg_acre_q30_2016!=., r fe cl(village) 
		outreg2 using "$path/replication/output/TableA20.doc", append ///
			ctitle(Respondent) bd(2) sd(2) nocons addtext(Village FE, YES)
		su res_work_days_acre_2014 if e(sample) & trt2==1
		mat A_3[1,1] = r(mean)
		mat A_3[2,1] = r(sd)
		test 2.trt2=3.trt2
		mat A_3[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat A_3[4,1] = r(p)
		test 3.trt2=4.trt2
		mat A_3[5,1] = r(p)	
		matmap A_3  AN_3, map(round(@,0.01))
		
	xtreg hh_work_days_acre_2016 i.trt2 hh_work_days_acre_2014 if gps_2016!=. /// 
	& mz_kg_acre_q30_2016!=., r fe cl(village) 
		outreg2 using "$path/replication/output/TableA20.doc", append ///
			ctitle(HH) bd(2) sd(2) nocons addtext(Village FE, YES)
		su hh_work_days_acre_2014 if e(sample) & trt2==1
		mat B_3[1,1] = r(mean)
		mat B_3[2,1] = r(sd)	
		test 2.trt2=3.trt2
		mat B_3[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat B_3[4,1] = r(p)
		test 3.trt2=4.trt2
		mat B_3[5,1] = r(p)	
		matmap B_3  BN_3, map(round(@,0.01))
		
	xtreg hired_work_days_acre_2016 i.trt2 hired_work_days_acre_2014 if gps_2016!=. ///
	& mz_kg_acre_q30_2016!=., r fe cl(village) 
		outreg2 using "$path/replication/output/TableA20.doc", append ///
			ctitle(Hired) bd(2) sd(2) nocons addtext(Village FE, YES)
		su hired_work_days_acre_2014 if e(sample) & trt2==1
		mat C_3[1,1] = r(mean)
		mat C_3[2,1] = r(sd)	
		test 2.trt2=3.trt2
		mat C_3[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat C_3[4,1] = r(p)
		test 3.trt2=4.trt2
		mat C_3[5,1] = r(p)	
		matmap C_3  CN_3, map(round(@,0.01))
		
	xtreg tot_work_days_acre_2016 i.trt2 tot_work_days_acre_2014 if gps_2016!=. ///
	& mz_kg_acre_q30_2016!=., r fe cl(village) 
		outreg2 using "$path/replication/output/TableA20.doc", append ///
			ctitle(All) bd(2) sd(2) nocons addtext(Village FE, YES)
		su tot_work_days_acre_2014 if e(sample) & trt2==1
		mat D_3[1,1] = r(mean)
		mat D_3[2,1] = r(sd)
		test 2.trt2=3.trt2
		mat D_3[3,1] = r(p)  
		test 2.trt2=4.trt2
		mat D_3[4,1] = r(p)
		test 3.trt2=4.trt2
		mat D_3[5,1] = r(p)	
		matmap D_3  DN_3, map(round(@,0.01))
	
	putexcel set "$path/replication/output/TableA20_F_tests.xls", sheet("F-tests") replace
	putexcel B2=matrix(AN_3) 
	putexcel C2=matrix(BN_3)
	putexcel D2=matrix(CN_3)
	putexcel E2=matrix(DN_3)
	
	putexcel B1 = "respondent"
	putexcel C1 = "hh"
	putexcel D1 = "hired"
	putexcel E1 = "total"
	putexcel A2 = "CONTROL MEAN"
	putexcel A3 = "CONTROL SD"
	putexcel A4 = "V_vs_R"
	putexcel A5 = "V_vs_RV"
	putexcel A6 = "R_vs_RV"
	
	erase "$path/replication/output/TableA19.txt"
	erase "$path/replication/output/TableA20.txt"
	
end


*Execute
main

*EOF
